﻿#include "logistics.h"
#include "ui_logistics.h"

Logistics::Logistics(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::Logistics)
{
    ui->setupUi(this);

    font=qApp->font();
    font.setPointSize(18);

    if(option.load(LST_LOGISTICS))
    {
        ui->express->addItems(option.getStringList());
        ui->express->addItem(OPT_EDIT);
        ui->express->setCurrentIndex(-1);
    }
    else
        ui->express->addItem(QStringLiteral("无法加载配置文件"));
}

Logistics::~Logistics()
{
    delete ui;
}

void Logistics::on_print_clicked()
{
    name=ui->contact->text();
    phone=ui->phone->text();
    addr=ui->address->toPlainText();
    if(name.isEmpty() || phone.isEmpty() || addr.isEmpty())
    {
        QMessageBox::critical(this,QStringLiteral("不能为空"),QStringLiteral("姓名、手机号、地址不能为空！"),QMessageBox::Ok);
        return;
    }
    else
    {
        name=QStringLiteral("收件人：") + name;
        phone=QStringLiteral("手机号：") + phone;
        addr=QStringLiteral("地址：") + addr;
        express=QStringLiteral("物流：")  + ui->express->currentText();
        num=QStringLiteral("件数：") + ui->number->text();
        remarks=QStringLiteral("备注：") +  ui->remarks->text();

        QString tmpPage=name + "\t\t" + phone + "\n" + addr + "\n"
                + express + "\t\t" + num + "\n" +  remarks + "\n";

        doc.setPlainText(tmpPage);
        doc.setDefaultFont(font);

        QPrinter printer;
        QPrintDialog printDialog(&printer,this);
        if(printDialog.exec() == QPrintDialog::Accepted)
            doc.print(&printer);
    }
}

void Logistics::reload(const OptionType &type)
{
    if(type == O_LOGISTICS)
    {
        if(option.load(LST_LOGISTICS))
        {
            ui->express->clear();
            ui->express->addItems(option.getStringList());
            ui->express->addItem(OPT_EDIT);
        }
    }
}

void Logistics::on_express_currentTextChanged(const QString &text)
{
    if(text == OPT_EDIT)
    {
        OptionEditor editor;
        if(editor.detect(O_LOGISTICS))
        {
            connect(&editor,SIGNAL(reload(OptionType)),this,SLOT(reload(OptionType)));
            if(editor.exec() == QDialog::Rejected)      //手动关闭，值为0
                ui->express->setCurrentIndex(-1);
        }
        else
            QMessageBox::critical(this,QStringLiteral("错误"),QStringLiteral("无法加载配置文件！"),QMessageBox::Ok);
    }
}
